﻿using System.Web;
using System.Collections.Generic;
using System.Linq;

using Tools.Json;

using SY.WebSite.BLL;
using SY.Uap.Entity;
using SY.WebSite.Entity;

namespace SY.WebSite.Web.Handler
{
    public class SuggestManageHandler : AdminWebHandler, IHttpHandler
    {
        public new void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            switch (Action)
            {
                case "getList":
                    GetList();
                    break;
                case "grant":
                    Grant();
                    break;
                case "forward":
                    Forward();
                    break;
                case "close":
                    Close();
                    break;
            }

            context.Response.End();
        }

        public void Close()
        {
            int id = Tools.Web.Request.GetFormInt("id", 0);

            if (id <= 0)
                BreakResponse();

            Suggest suggest = BizSuggest.GetByKey(id);
            if (suggest == null || suggest.Status == 3)
                BreakResponse();

            suggest.Status = 3;
            suggest.ControlUserId = CurrentUser.UserId;
            suggest.ControlUserName = CurrentUser.UserName;

            int result = BizSuggest.Update(suggest);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                return;
            }
            BreakResponse("服务器异常，请稍后再试");
        }

        public void Forward()
        {
            int id = Tools.Web.Request.GetFormInt("id", 0);
            int toUserId = Tools.Web.Request.GetFormInt("toUserId", 0);

            string relateDeptName = Tools.Web.Request.GetFormString("relateDeptName");
            string controlComment = Tools.Web.Request.GetFormString("controlComment");

            if (id <= 0 || toUserId <= 0 || string.IsNullOrEmpty(relateDeptName))
                BreakResponse();

            Suggest suggest = BizSuggest.GetByKey(id);
            if (suggest == null || suggest.Status == 3)
                BreakResponse();

            User toUser = BizUser.GetByKey(toUserId);
            if (toUser == null || toUser.Status < 0)
                BreakResponse();

            suggest.ToUserId = toUser.UserId;
            suggest.ToUserName = toUser.UserName;
            suggest.RelateDeptName = relateDeptName;
            suggest.Status = 1;
            suggest.ControlUserId = CurrentUser.UserId;
            suggest.ControlUserName = CurrentUser.UserName;
            suggest.ControlComment = Tools.StringUtil.HtmlEncode(controlComment);

            int result = BizSuggest.Update(suggest);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                return;
            }
            BreakResponse("服务器异常，请稍后再试");
        }

        public void Grant()
        {
            int type = Tools.Web.Request.GetFormInt("type", 0);
            int amount = Tools.Web.Request.GetFormInt("amount", 0);
            int id = Tools.Web.Request.GetFormInt("id", 0);

            if (type <= 0 || amount <= 0 || id <= 0)
                BreakResponse();

            Suggest suggest = BizSuggest.GetByKey(id);
            if (suggest == null || suggest.Type > 0)
                BreakResponse();

            suggest.Type = type;
            suggest.Amount = amount;
            suggest.ControlUserId = CurrentUser.UserId;
            suggest.ControlUserName = CurrentUser.UserName;

            int result = BizSuggest.Update(suggest);
            if (result > 0)
            {
                if (type == 1)
                {
                    IntegrationTransaction transaction = new IntegrationTransaction
                    {
                        ToUserId = suggest.UserId,
                        ToUserName = suggest.UserName,
                        Comment = "参与建议奖励",
                        TargetId = suggest.Id,
                        Amount = amount,
                        Type = 1
                    };
                    BizIntegrationTransaction.Add(transaction);

                    Integration integration = BizIntegration.GetByKey(suggest.UserId);
                    integration.Available += amount;
                    BizIntegration.Update(integration);
                }

                Response.Write(new JsonTransfer().Add("success", ""));
                return;
            }
            BreakResponse("服务器异常，请稍后再试");
        }

        public void GetList()
        {
            int pageIndex = RequestIntParam("pageIndex");
            int pageSize = RequestIntParam("pageSize", 20);

            string deptName = RequestStringParam("deptName");
            string relateDeptName = RequestStringParam("relateDeptName");
            int status = RequestIntParam("status", 100);

            if (status < 0)
                BreakResponse();

            JsonTransfer jt = new JsonTransfer();
            IEnumerable<Suggest> list = BizSuggest.QueryRelateDeptName(status, deptName, relateDeptName);

            jt.Add("list", list.Skip(pageIndex*pageSize).Take(pageSize));
            jt.Add("count", list.Count());
            Response.Write(jt);
        }
    }
}